package com.summer.obj;

import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;

public class CloseTest {

	public static void main(String[] args) {
		
		CloseTest.list.add(new TestObj());
		CloseTest.list.add(new TestObj1());
		CloseTest.list.add(new TestObj1());
		CloseTest.list.add(new TestObj1());
		CloseTest.list.add(new TestObj1());
		
		
//		try(TestObj obj = new TestObj()) {
//			System.out.println(obj.toString());
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}

		System.out.println(list.size());
		
		for(int index = list.size()-1; index >= 0; index--) {
			try(Closeable obj = (Closeable) list.get(index)) {
				System.out.println(obj.toString());
//				throw new RuntimeException();
			}  catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println(list.size());
	}
	
	public final static ArrayList<Object> list = new ArrayList();
	
	public static class TestObj implements Closeable {

		@Override
		public void close() throws IOException {
			CloseTest.list.remove(this);
			System.out.println("TestObj close");
		}
		
	}
	
	public interface AouteClose extends Closeable {
		
		default void close() throws IOException {
			CloseTest.list.remove(this);
			System.out.println("AouteClose close");
		}
		
	}
	
	public static class TestObj1 implements AouteClose {
		
	}
	
}
